package com.maycur.ctrip.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.maycur.ctrip.domain.FeeData;
import com.maycur.ctrip.domain.FeeDynamicField;
import com.maycur.ctrip.enumeration.FeeDynamicFieldCode;
import com.maycur.ctrip.enumeration.FeeSourceType;
import com.maycur.ctrip.enumeration.FeeTypeGroupTag;
import com.maycur.ctrip.enumeration.PayType;
import com.maycur.ctrip.util.Constant;
import com.maycur.ice.ctrip.HotelOrderDto;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by JoeyXin on 6/29/16.
 */
public class HotelOrderInfoEntity {

    @JsonProperty(value = "OrderID")
    private String orderId;

    @JsonProperty(value = "JourneyNo")
    private String journeyNo;

    @JsonProperty(value = "UID")
    private String uId;

    @JsonProperty(value = "PreEmployeeID")
    private String eId;

    @JsonProperty(value = "PreEmployeeName")
    private String eName;

    //总价(前支付币种的订单总金额)
    @JsonProperty(value = "Amount")
    private BigDecimal consumeAmount;

    //总价币种
    @JsonProperty(value = "Currency")
    private String consumeCcy;

    //总金额(consumeAmount 转换为人民币币种的金额)
    @JsonProperty(value = "AmountRMB")
    private BigDecimal consumeAmountCny;

    //快递费
    @JsonProperty(value = "PostAmount")
    private BigDecimal postAmount;

    //酒店类型:协议酒店, 会员酒店
    @JsonProperty(value = "HotelType")
    private String hotelType;

    //酒店名称
    @JsonProperty(value = "HotelName")
    private String hotelName;

    //酒店英文名称
    @JsonProperty(value = "HotelEnName")
    private String hotelNameEN;

    //酒店星级
    @JsonProperty(value = "Star")
    private Integer hotelStarLevel;

    @JsonProperty(value = "Telephone")
    private String hotelPhone;

    @JsonProperty(value = "Address")
    private String hotelAddress;

    @JsonProperty(value = "OrderStatus")
    private String orderStatus;

    //授权状态
    @JsonProperty(value = "AuthorizeStatus")
    private String auditedStatus;

    //下单时间
    @JsonProperty(value = "OrderDate")
    private Date orderedAt;

    //入住时间
    @JsonProperty(value = "StartTime")
    private Date checkedInAt;

    //离开时间
    @JsonProperty(value = "EndTime")
    private Date checkedOutAt;

    //最晚取消时间
    @JsonProperty(value = "LastCancelTime")
    private Date lastCancelAt;

    //主账户:公司预存款账号,用于结算
    @JsonProperty(value = "FundAccount")
    private String fundAccount;

    //子账户:公司预存款账号的子账号,用于结算
    @JsonProperty(value = "SubAccount")
    private String subAccount;

    //支付方式:现金;公司账户;礼品卡支付;信用卡支付
    @JsonProperty(value = "PayType")
    private String payType;

    //支付类型:前台现付;预付
    @JsonProperty(value = "BalanceType")
    private String balanceType;

    //公费;私费
    @JsonProperty(value = "Corp_payType")
    private String purpose;

    //省、直辖市英文名
    @JsonProperty(value = "ProvinceEname")
    private String provinceEN;

    //城市名称
    @JsonProperty(value = "CityName")
    private String cityName;

    //入住人姓名,如果多个入住人,会用','分隔
    @JsonProperty(value = "ClientName")
    private String clientName;

    @JsonProperty(value = "ContactEmail")
    private String contactEmail;

    @JsonProperty(value = "ContactName")
    private String contactName;

    @JsonProperty(value = "ContactTel")
    private String contactPhone;

    @JsonProperty(value = "CostCenter")
    private String costCenter1;

    @JsonProperty(value = "CostCenter2")
    private String costCenter2;

    @JsonProperty(value = "CostCenter3")
    private String costCenter3;

    //金额差标RC Code: 客户公司低价控制代码
    @JsonProperty(value = "LowPriceRC")
    private String lowPriceRC;

    //金额差标RC: 客户公司低价控制内容
    @JsonProperty(value = "LowPriceRCInfo")
    private String lowPriceRCInfo;

    //协议RC Code: 客户公司协议控制代码
    @JsonProperty(value = "AgreementRC")
    private String agreementRC;

    //协议RC: 客户公司协议控制内容
    @JsonProperty(value = "AgreementRCInfo")
    private String agreementRCInfo;

    //房型名称
    @JsonProperty(value = "RoomName")
    private String roomName;

    //房间数
    @JsonProperty(value = "RoomQuantity")
    private Integer roomQuantity;

    //间夜数
    @JsonProperty(value = "RoomDays")
    private Integer roomDays;

    //夜审数据
    @JsonProperty(value = "AuditInfo")
    private HotelRoomAuditInfo auditInfo;

    //入住人信息
    @JsonProperty(value = "ClientInfo")
    private List<HotelClientInfoEntity> clientInfoEntities;

    @JsonProperty(value = "RoomInfo")
    private List<HotelRoomInfoEntity> roomInfoEntities;

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getJourneyNo() {
        return journeyNo;
    }

    public void setJourneyNo(String journeyNo) {
        this.journeyNo = journeyNo;
    }

    public String getuId() {
        return uId;
    }

    public void setuId(String uId) {
        this.uId = uId;
    }

    public String geteId() {
        return eId;
    }

    public void seteId(String eId) {
        this.eId = eId;
    }

    public String geteName() {
        return eName;
    }

    public void seteName(String eName) {
        this.eName = eName;
    }

    public BigDecimal getConsumeAmount() {
        return consumeAmount;
    }

    public void setConsumeAmount(BigDecimal consumeAmount) {
        this.consumeAmount = consumeAmount;
    }

    public String getConsumeCcy() {
        return consumeCcy;
    }

    public void setConsumeCcy(String consumeCcy) {
        this.consumeCcy = consumeCcy;
    }

    public BigDecimal getConsumeAmountCny() {
        return consumeAmountCny;
    }

    public void setConsumeAmountCny(BigDecimal consumeAmountCny) {
        this.consumeAmountCny = consumeAmountCny;
    }

    public BigDecimal getPostAmount() {
        return postAmount;
    }

    public void setPostAmount(BigDecimal postAmount) {
        this.postAmount = postAmount;
    }

    public String getHotelType() {
        return hotelType;
    }

    public void setHotelType(String hotelType) {
        this.hotelType = hotelType;
    }

    public String getHotelName() {
        return hotelName;
    }

    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }

    public String getHotelNameEN() {
        return hotelNameEN;
    }

    public void setHotelNameEN(String hotelNameEN) {
        this.hotelNameEN = hotelNameEN;
    }

    public Integer getHotelStarLevel() {
        return hotelStarLevel;
    }

    public void setHotelStarLevel(Integer hotelStarLevel) {
        this.hotelStarLevel = hotelStarLevel;
    }

    public String getHotelPhone() {
        return hotelPhone;
    }

    public void setHotelPhone(String hotelPhone) {
        this.hotelPhone = hotelPhone;
    }

    public String getHotelAddress() {
        return hotelAddress;
    }

    public void setHotelAddress(String hotelAddress) {
        this.hotelAddress = hotelAddress;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getAuditedStatus() {
        return auditedStatus;
    }

    public void setAuditedStatus(String auditedStatus) {
        this.auditedStatus = auditedStatus;
    }

    public Date getOrderedAt() {
        return orderedAt;
    }

    public void setOrderedAt(Date orderedAt) {
        this.orderedAt = orderedAt;
    }

    public Date getCheckedInAt() {
        return checkedInAt;
    }

    public void setCheckedInAt(Date checkedInAt) {
        this.checkedInAt = checkedInAt;
    }

    public Date getCheckedOutAt() {
        return checkedOutAt;
    }

    public void setCheckedOutAt(Date checkedOutAt) {
        this.checkedOutAt = checkedOutAt;
    }

    public Date getLastCancelAt() {
        return lastCancelAt;
    }

    public void setLastCancelAt(Date lastCancelAt) {
        this.lastCancelAt = lastCancelAt;
    }

    public String getFundAccount() {
        return fundAccount;
    }

    public void setFundAccount(String fundAccount) {
        this.fundAccount = fundAccount;
    }

    public String getSubAccount() {
        return subAccount;
    }

    public void setSubAccount(String subAccount) {
        this.subAccount = subAccount;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public String getBalanceType() {
        return balanceType;
    }

    public void setBalanceType(String balanceType) {
        this.balanceType = balanceType;
    }

    public String getPurpose() {
        return purpose;
    }

    public void setPurpose(String purpose) {
        this.purpose = purpose;
    }

    public String getProvinceEN() {
        return provinceEN;
    }

    public void setProvinceEN(String provinceEN) {
        this.provinceEN = provinceEN;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public String getCostCenter1() {
        return costCenter1;
    }

    public void setCostCenter1(String costCenter1) {
        this.costCenter1 = costCenter1;
    }

    public String getCostCenter2() {
        return costCenter2;
    }

    public void setCostCenter2(String costCenter2) {
        this.costCenter2 = costCenter2;
    }

    public String getCostCenter3() {
        return costCenter3;
    }

    public void setCostCenter3(String costCenter3) {
        this.costCenter3 = costCenter3;
    }

    public String getLowPriceRC() {
        return lowPriceRC;
    }

    public void setLowPriceRC(String lowPriceRC) {
        this.lowPriceRC = lowPriceRC;
    }

    public String getLowPriceRCInfo() {
        return lowPriceRCInfo;
    }

    public void setLowPriceRCInfo(String lowPriceRCInfo) {
        this.lowPriceRCInfo = lowPriceRCInfo;
    }

    public String getAgreementRC() {
        return agreementRC;
    }

    public void setAgreementRC(String agreementRC) {
        this.agreementRC = agreementRC;
    }

    public String getAgreementRCInfo() {
        return agreementRCInfo;
    }

    public void setAgreementRCInfo(String agreementRCInfo) {
        this.agreementRCInfo = agreementRCInfo;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public Integer getRoomQuantity() {
        return roomQuantity;
    }

    public void setRoomQuantity(Integer roomQuantity) {
        this.roomQuantity = roomQuantity;
    }

    public Integer getRoomDays() {
        return roomDays;
    }

    public void setRoomDays(Integer roomDays) {
        this.roomDays = roomDays;
    }

    public HotelRoomAuditInfo getAuditInfo() {
        return auditInfo;
    }

    public void setAuditInfo(HotelRoomAuditInfo auditInfo) {
        this.auditInfo = auditInfo;
    }

    public List<HotelClientInfoEntity> getClientInfoEntities() {
        return clientInfoEntities;
    }

    public void setClientInfoEntities(List<HotelClientInfoEntity> clientInfoEntities) {
        this.clientInfoEntities = clientInfoEntities;
    }

    public List<HotelRoomInfoEntity> getRoomInfoEntities() {
        return roomInfoEntities;
    }

    public void setRoomInfoEntities(List<HotelRoomInfoEntity> roomInfoEntities) {
        this.roomInfoEntities = roomInfoEntities;
    }

    public HotelOrderDto convertToOrderDto() {
        HotelOrderDto hotelOrderDto = new HotelOrderDto();

        hotelOrderDto.orderId = this.orderId;
        hotelOrderDto.journeyNo = this.journeyNo;

        hotelOrderDto.hotelName = this.hotelName;//酒店名称
        hotelOrderDto.roomQuantity = this.roomQuantity; //房间数
        hotelOrderDto.roomName = this.roomName; //房型名称
        hotelOrderDto.consumeAmount = this.consumeAmount.floatValue();
        hotelOrderDto.consumeCcy = this.consumeCcy;
        hotelOrderDto.consumeDateTime = new DateTime(this.orderedAt).toString(Constant.DATE_TIME_FORMAT);
        hotelOrderDto.consumeCity = this.cityName;
        hotelOrderDto.checkedInDateTime = new DateTime(this.checkedInAt).toString(Constant.DATE_TIME_FORMAT);
        hotelOrderDto.checkedOutDateTime = new DateTime(this.checkedOutAt).toString(Constant.DATE_TIME_FORMAT);

        hotelOrderDto.orderStatus = this.orderStatus;
        hotelOrderDto.auditedStatus = this.auditedStatus;



/*        hotelOrderDto.hotelRoomDtoSeq = new HotelRoomDto[roomInfoEntities.size()];

        for(int i = 0; i< roomInfoEntities.size(); i++) {
            HotelRoomDto hotelRoomDto = new HotelRoomDto();

            hotelRoomDto.roomName = roomInfoEntities.get(i).getRoomName(); //房型名称
            hotelRoomDto.bedType = roomInfoEntities.get(i).getBedType();//床型
            hotelRoomDto.consumeAmount = roomInfoEntities.get(i).getAmount().floatValue();//单价
            hotelRoomDto.checkedInDateTime = new DateTime(roomInfoEntities.get(i).getCheckedInAt()).toString(Constant.DATE_TIME_FORMAT);
            hotelRoomDto.checkedOutDateTime = new DateTime(roomInfoEntities.get(i).getCheckedOutAt()).toString(Constant.DATE_TIME_FORMAT);

            hotelOrderDto.hotelRoomDtoSeq[i] = hotelRoomDto;
        }*/

        return hotelOrderDto;

    }

    public FeeData extractFee(String entCode, String feeCode, String userCode){
        FeeData feeData = new FeeData();

        feeData.setEntCode(entCode);
        feeData.setFeeCode(feeCode);
        feeData.setUserCode(userCode);

        //默认设置费用类型为员工住宿类型
        feeData.setFeeTypeCode(Constant.DEFAULT_ACCOMMODATION_FEE_TYPE);

        //消费金额
        //支付方式1 现金, 2 信用卡 ,3 支付宝, 4 借记卡, 5 其他
        feeData.setPayMethod(PayType.fromDesc(payType));
        feeData.setConsumeAmount(consumeAmount);
        feeData.setConsumeCcy(Constant.DEFAULT_CTRIP_CURRENCY);
        feeData.setConsumeTime(orderedAt);
        feeData.setSourceId(orderId);
        feeData.setSourceType(FeeSourceType.CTRIP.getCode());
        feeData.setStatus(Constant.DEFAULT_FEE_STATUS);
        feeData.setFeeTypeGroupTags(FeeTypeGroupTag.Accommodation);

        feeData.setBaseCcy(Constant.DEFAULT_CTRIP_CURRENCY);
        feeData.setExchangeRate(Constant.DEFAULT_EXCHANGE_RATE);
        feeData.setSysExchangeRate(Constant.DEFAULT_EXCHANGE_RATE);
        feeData.setFeeAmount(consumeAmount);
        feeData.setApprovedAmount(consumeAmount);

        feeData.setValid(true);
        return feeData;
    }

    public List<FeeDynamicField> extractFeeDynamicFields(String entCode, String feeCode) {
        List<FeeDynamicField> feeDynamicFields = new ArrayList<>();

        for(FeeDynamicFieldCode dynamicFieldCode:FeeDynamicFieldCode.getAccomFieldCodes()){
            FeeDynamicField dynamicField = new FeeDynamicField();
            dynamicField.setEntCode(entCode);
            dynamicField.setFeeCode(feeCode);
            dynamicField.setFeeTypeCode(Constant.DEFAULT_ACCOMMODATION_FEE_TYPE);
            dynamicField.setFeeKind(Constant.DEFAULT_FEE_KIND);
            dynamicField.setFeeDetailChooseCode(dynamicFieldCode.getCode());

            switch(dynamicFieldCode) {
                case ACCOM_CONSUMPTION_PLATFORM:
                    dynamicField.setFeeDetailChooseValue(Constant.DEFAULT_CONSUMPTION_PLATFORM);
                    break;
                case ACCOM_HOTEL_NAME:
                    dynamicField.setFeeDetailChooseValue(hotelName);
                    break;
                case ACCOM_CHECKED_IN_TIME:
                    dynamicField.setFeeDetailChooseValue(new DateTime(checkedInAt).toString(Constant.DATE_TIME_FORMAT));
                    break;
                case ACCOM_CHECKED_OUT_TIME:
                    dynamicField.setFeeDetailChooseValue(new DateTime(checkedOutAt).toString(Constant.DATE_TIME_FORMAT));
                    break;
                case ACCOM_ROOM_NAME:
                    dynamicField.setFeeDetailChooseValue(roomName);
                    break;
                case ACCOM_ROOM_DAYS:
                    dynamicField.setFeeDetailChooseValue(roomDays.toString());
                    break;
            }

            feeDynamicFields.add(dynamicField);
        }

        return feeDynamicFields;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
